//
//  TitlesSettingVC.m
//  GcwSDKDemo
//
//  Created by 陈超 on 2016/12/30.
//  Copyright © 2016年 陈超. All rights reserved.
//

#import "TitlesSettingVC.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "VideoEffectUtil.h"
#import "VideoPlayBackVC.h"
@implementation TitlesSettingVC
-(void)viewDidLoad{
    [super viewDidLoad];
    self.titlesModel=[TitlesModel new];
    AVURLAsset *videoAsset=[AVURLAsset assetWithURL:self.inputUrl];
    Float64 videoInterVal=CMTimeGetSeconds(videoAsset.duration);
    [VideoEffectUtil sharedInstance].bitRate=[self.bitRate.text floatValue];
    Float64 fileSize=videoInterVal*[VideoEffectUtil sharedInstance].bitRate/800000;
    self.fileSize.text=[NSString stringWithFormat:@"%.2fM",fileSize];
}
- (IBAction)onTextChanged:(UITextField *)sender {
    self.titlesLabel.text=[NSString stringWithFormat:@"表演者：%@",sender.text];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}
- (IBAction)onClickIgnoreButton:(UIButton *)sender {

    [self toMerge];

}

- (IBAction)onClickCompleteButton:(id)sender {
    [self toMerge];
}
-(void)toMerge{
    __weak typeof(self) weakSelf=self;
    
    //设置含特效的视频路径
    [VideoEffectUtil sharedInstance].outPutVideoUrl=[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:
                                                                            @"myVideo.mp4"]];
    
    //待添加特效的视频路径
    NSURL *inPutVideoUrl=self.inputUrl;
    
    //生成含特效的视频
    self.titlesModel.view=self.titlesView;
    self.titlesModel.duration=1.0f;
    [[VideoEffectUtil sharedInstance] addEffect:inPutVideoUrl bgMusicUrl:self.mp3Url lyricData:nil borderModel:self.borderModel cammeraTranfromModel:self.tranformModel titlesModel:self.titlesModel progressBlock:^(float prog) {
        NSLog(@"progress: %f",prog);
        dispatch_async(dispatch_get_main_queue(), ^{
            //注意在主线程更新UI
            float percent=prog*100;
            weakSelf.progressLabel.text=[NSString stringWithFormat:@"%.1f%%",percent];
        });
    } completeBlock:^(NSURL *url) {
        
        
        VideoPlayBackVC *videoPlayerBackVC=[[VideoPlayBackVC alloc] initWithParams:url];
        [self.navigationController pushViewController:videoPlayerBackVC animated:YES];
    } failureBlock:^(NSError *error) {
        //磁盘空间不足等会在这里报错
        
        NSLog(@"error: %@",error);
    }];

}
- (IBAction)bitRateChanged:(UITextField *)sender {
    AVURLAsset *videoAsset=[AVURLAsset assetWithURL:self.inputUrl];
    Float64 videoInterVal=CMTimeGetSeconds(videoAsset.duration);
    [VideoEffectUtil sharedInstance].bitRate=[sender.text floatValue];
    NSInteger fileSize=videoInterVal*[VideoEffectUtil sharedInstance].bitRate/8000000;
    self.fileSize.text=[NSString stringWithFormat:@"%.2fM",fileSize];
}

-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
-(BOOL)checkAlbumAuthorization{
    ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
    if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied)
    {
        //无权限
        
        return NO;
    }
    return YES;
}

@end
